/* On{X} Script: Notify when battery is charged to 98% or above every 5 minutes
 *
 * Developer: Gaurav Nandwani
 *
 * Description:
 *
 * This rule monitors battery updates and notifies when:
 * The battery is at 98% or greater and the last known percentage was less than 98%
 * OR The battery is at 98% and the last notification was 5 ago and the battery is still charging
 * 
 * The script is configurable where battery% and gap between notifications can be defined.
 * 
 * Usage:
 * 
 * Change following variables as needed:
 * 
 * notifyOnBatteryLevel	- Notifies when the battery % is this value.
 * notifyAfterEveryMins	- Notify again after this this interval (in minutes) and if battery % is notifyOnBatteryLevel
 * notificationSubject	- Short Notification Subject - Shown in bold in notification message
 */

// Initializing variables

var notifyOnBatteryLevel = 98;
var notifyAfterEveryMins = 5; 
var notificationSubject = "Battery Charged";

// Finished Initializing Variables

console.log('Started script: Notify when battery is charged to >= ' + notifyOnBatteryLevel + '%.');

// Subscribe to battery updates
device.battery.on('updated', function (signal) {
    var currentBatteryPercent = signal.percentage;
    var now = new Date().getTime();
    
    // Get the last seen percentage, default to the current percentage if have not seen any
    var lastBatteryPercent = device.localStorage.getItem('lastBatteryPercent') || currentBatteryPercent;
    lastBatteryPercent = JSON.parse(lastBatteryPercent);
    
    // Read time when last notified
    var lastNotified = device.localStorage.getItem('lastNotified') || now;
    lastNotified = JSON.parse(lastNotified);
    
    // Get time difference between last notification & now and convert to minutes
    var notificationTimeDiff = Math.floor((now - lastNotified) / 1000 / 60, 10);
    
    // Raise battery charged notification when: 
    // The battery is at or higher than notifyOnBatteryLevel% and the last known percentage was less than notifyOnBatteryLevel%
    // OR The battery is at notifyOnBatteryLevel% and the last notification was notifyAfterEveryMins ago and the battery is still charging
    if ((lastBatteryPercent < notifyOnBatteryLevel && currentBatteryPercent >= notifyOnBatteryLevel) || 
    (signal.isCharging && currentBatteryPercent === notifyOnBatteryLevel && notificationTimeDiff >= notifyAfterEveryMins)) {
        // Show the notification 
        var notification = device.notifications.createNotification(notificationSubject);
        notification.content = 'Battery is charged to ' + notifyOnBatteryLevel + '%';
        notification.show();
        // Notified: Save notification time
        device.localStorage.setItem('lastNotified', JSON.stringify(now));
        console.info('Notified: ' + notificationSubject + ' - ' + 'Battery is charged to ' + currentBatteryPercent + '%');
    }
    
    // Save the current battery percentage
    device.localStorage.setItem('lastBatteryPercent', JSON.stringify(currentBatteryPercent));
});

// Script completed
console.log('Completed script: Notify when battery is charged to >= ' + notifyOnBatteryLevel + '%.');
